package com.yancy.controller;

import com.yancy.entity.New;
import com.yancy.service.NewService;
import com.yancy.service.impl.NewServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description:
 * @author: YancyZhang
 * @time: 2021/11/26 15:11
 */
@WebServlet("/admin")
public class AdminNewServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("method");
        //删除
        if(type .equals("delete")){
            PrintWriter out = resp.getWriter();
            out.print(this.delete(req,resp));
        }
        //更新
        else if (type.equals("update")){
            PrintWriter out = resp.getWriter();
            try {
                out.print(this.update(req,resp));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if(type.equals("add")){//新增新闻
            PrintWriter out = resp.getWriter();
            out.print(this.add(req,resp));
        }
    }

    protected boolean delete(HttpServletRequest request,HttpServletResponse response){
        NewService newService = new NewServiceImpl();
        return newService.delete(request.getParameter("id"));
    }

    protected boolean update(HttpServletRequest request,HttpServletResponse response) throws ParseException {
        New updateNew = new New(request.getParameter("id"),request.getParameter("title"),request.getParameter("content"),request.getParameter("type"));
        NewService newService = new NewServiceImpl();
        return newService.update(updateNew);
    }

    protected boolean add(HttpServletRequest request,HttpServletResponse response){
        New addNew = new New(request.getParameter("title"),request.getParameter("content"),request.getParameter("type"));
        NewService newService = new NewServiceImpl();
        return newService.add(addNew);
    }
}